package com.hjm.ui.comment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hjm.ui.R;

public class EditCommentFragment extends Fragment {
    private TextView commentEditTitle;
    private EditText commentEditText;
    private Button commentEditButton;
    private RatingBar descRatingBar,fuwuRatingBar,wuliuRatingBar;
    private OnEditCommentListener onEditCommentListener;
    private String title;

    public void setOnEditStoreCommentListener(OnEditCommentListener onEditCommentListener) {
        this.onEditCommentListener = onEditCommentListener;
    }

    public static EditCommentFragment newInstance(String title) {
        EditCommentFragment editCommentFragment = new EditCommentFragment();
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        editCommentFragment.setArguments(bundle);
        return editCommentFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null){
            title = getArguments().getString("title");
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_comment_edit, container, false);

        commentEditTitle = view.findViewById(R.id.comment_edit_title);
        if (title != null)
            commentEditTitle.setText(title);

        commentEditText = view.findViewById(R.id.comment_edit_content);
        commentEditButton = view.findViewById(R.id.comment_edit_submit);

        commentEditButton.setOnClickListener((v)->{
           if (onEditCommentListener != null) {
               int[] rating = new int[]{
                       (int) descRatingBar.getRating(),
                       (int) fuwuRatingBar.getRating(),
                       (int) wuliuRatingBar.getRating()};
               onEditCommentListener.onEditStoreCommentCommit(commentEditText.getText().toString(),rating);
           }
        });

        // 初始化评价控件
        descRatingBar = view.findViewById(R.id.rating_bar_desc);
        fuwuRatingBar = view.findViewById(R.id.rating_bar_fuwu);
        wuliuRatingBar = view.findViewById(R.id.rating_bar_wuliu);
        return view;
    }

    public interface OnEditCommentListener {
        void onEditStoreCommentCommit(String comment,int[] rating);
    }
}
